function setModalSize(){
	var documentW = $(window).width();
	var documentH = $(window).height();
	$('#modalW').width(documentW * 0.6);
	$('#modalH').height(documentH * 0.7);	
}

jQuery(document).ready(function() {
	$.ajax({
		async : false,
		url : projectPath + "user/getCurrentUser.do",
		data : {},
		dataType : "json",
		success : function(data) {
			$('#org').val(data.orgname);
			$('#org').attr('data', data.orgcode);
		}
	});
	initTable();
	
	App.setPage("user");  //Set current page
	App.init(); //Initialise plugins and elements
	

	//绑定机构下拉列表
	bindOrg2('org');

	//列表查询事件
	$('#search').click(function(){
		$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	
	
	//新增
	$('#addDeclare').click(function(){
    	setModalSize();
    	
    	$('#ifModal').attr("src", "add.html");
    	
		$('#modalTile').html("政务项目新增");
		$("#modal").modal('show');
	});
	
	//保存数据事件
	$('#save').click(function(){
		$("#ifModal")[0].contentWindow.save();
	});
	
	
	//清空表单数据事件
	$("#modalDeclear").on("hidden.bs.modal", function() {
      	
	});
	

	//批量删除
	$('#batchDelete').click(function(){
		var data = $('#table').bootstrapTable('getSelections');
		
		var itemnos = "";
		for(var i = 0; i < data.length; i++){
			itemnos += data[i].itemno + ",";
		}
		
		itemnos = itemnos.substring(0, itemnos.length - 1);
		
        $.confirm({
            title: '操作提示！',
    	    content: '是否<strong>批量删除</strong>政务项目数据?<br /><span style="color:red;">['+ itemnos +']</span>',
            buttons: {
                ok: {
                    text: "确定",
                    btnClass: 'btn-primary',
                    keys: ['enter'],
                    action: function(){
        				$.ajax({ 
        					url: basePath() + 'declare/batchDelete.do',
        					data: {"itemnos": itemnos},
        					dataType: "json",
        					success: function(data){	            						
        						if(data.result == 1){        							        							
    		        	            $.alert('删除政务项目['+ data.datas[0].itemno +']成功!');
        				        	$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
        						}
        			      	}
        				});
                    }
                },
                cancel: {
                    text: "取消",
                    keys: ['esc']
                }
            }
        })
	})
});		



function initTable(){		
	$('#table').bootstrapTable({
		method: 'post',
	    url: basePath() + 'user/list.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
                	pageNum: params.offset / params.limit + 1,
                	pageSize: params.limit,
                	username: $('#username').val(),
                	orgcode: $('#org').attr('data')
                };
        },
	    columns: [{
            field : 'checked',
            checkbox : true

        }, {  
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '80px',
            formatter : function(value, row, index) {  
// 		                return index + 1;  
                var page = $('#table').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }  
        }, {
	        field: 'userid',
	        title: '用户编号'
	    }, {
	        field: 'username',
	        title: '用户名称'
	    }, {
	        field: 'orgname',
	        title: '所属机构'
	    }, {
	        field: 'deptname',
	        title: '所属部门'
	    },  {
            field: 'operate',
            title: '操作',
            align: 'center',
//            width: '400px',
            events: operateEvents,
            formatter: operateFormatter
        } ]
	});
}

function operateFormatter(value, row, index) {
    return [
            '<button type="button" class="Right btn btn-success  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-user"></span>角色授权</button>'
    ].join('');
}


window.operateEvents = {
    'click .Right': function (e, value, row, index) {		//列表操作栏【角色授权】
    	setModalSize();

    	var userid = row.userid;
    	$('#ifModal').attr("src", "right.html?userid=" + userid);
    	
		$('#modalTile').html("角色授权");
		$("#modal").modal('show');
    }
 };

function check(data){	
	var result = true;
	

	if($('#tableDictDetail').find('tbody').find('tr').not('.no-records-found').length == 0){
		$.alert('请增加<span style="color:red;"><strong>字典明细</strong></span>的行数据！');
		
		return false;
	}
	
	
	var mustStr = "dictcode,dictname";
	
	var strMsg = "";
	var mustStrs = mustStr.split(',');
	for(var i=0; i<mustStrs.length; i++){
		var obj = $('[name="'+mustStrs[i]+'"]');
		if(obj.val() == ""){
			strMsg += obj.attr("placeholder") + "！<br />";
			
			result = false;
		}
	}
		
	
	if(strMsg != ""){
		$.alert(strMsg);
	}
	
	return result;
}

function getData(id){
	var allData =  $('#table').bootstrapTable('getData');
	for(var i = 0; i < allData.length; i++){
		if(allData[i].userid == id)
			return allData[i];
	}
}